#include "ALL_Init.h"

MYADC1_Typedef MYADC1;
MYADC2_Typedef MYADC2;
I_abc_Typedef I_abc;

uint32_t DAC_data = 2047;//基准电压矫正

void Everying_Init(void)
{
    //DWT初始化
    DWT_Init(168);

    //初始化ADC
    bsp_adc_init(&MYADC1, &MYADC2);

    //初始化DAC
    HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, DAC_data);
    HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);

    //初始化CAN
    can_bsp_init();

    LCD_Init();//LCD初始化
    LCD_Fill(0,0,LCD_W,LCD_H,BLACK);
}

void User_Loop(void)
{
    uint32_t time1 = 0, time2 = 0;

    if(HAL_GetTick() - time1 > 200)
    {
        HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
        time1 = HAL_GetTick();
    }

    if(HAL_GetTick() - time2 > 500)
    {
        HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_14);
        time2 = HAL_GetTick();
    }

	bsp_adc1_read(&MYADC1);
	bsp_adc2_read(&MYADC2);
	
    LCD_ShowString(10, 5,(uint8_t *)"[READY]", GREEN, BLACK, 32, 0);

    LCD_ShowString(130, 5,(uint8_t *)"Vbus:", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(170, 5, MYADC2.ADC_Channel.VBUS_f, 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(220, 5, (uint8_t *)"V", WHITE, BLACK, 16, 0);

    LCD_ShowString(130, 21,(uint8_t *)"Ibus:", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(170, 21, (float)MYADC1.ADC_Channel.IBUS_f, 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(220, 21, (uint8_t *)"A", WHITE, BLACK, 16, 0);

    LCD_ShowString(130, 37,(uint8_t *)"Tmos:", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(170, 37, (MYADC2.ADC_Channel.NTC2_f+MYADC1.ADC_Channel.NTC1_f)/2.0f, 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(220, 37, (uint8_t *)"C", WHITE, BLACK, 16, 0);

    LCD_DrawLine(10, 60, 10, 135, WHITE);
    LCD_DrawLine(230, 60, 230, 135, WHITE);

    LCD_ShowString(20, 40, (uint8_t *)"Mode :", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 60, (uint8_t *)"Iq.  :", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 80, (uint8_t *)"Vel. :", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 100,(uint8_t *)"Pos. :", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 120,(uint8_t *)"Tor. :", WHITE, BLACK, 16, 0);

    LCD_ShowString(76, 40, (uint8_t *)"[Vel.]", WHITE, BLACK, 16, 0);

    LCD_ShowFloatNum1(68, 60, MATH_ABS_float(0), 3, 2, BRRED, BLACK, 16);
    LCD_ShowString(124, 60, (uint8_t *)"->", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(140, 60, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(196, 60, (uint8_t *)"A", WHITE, BLACK, 16, 0);

    LCD_ShowFloatNum1(68, 80, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(124, 80, (uint8_t *)"->", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(140, 80, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(196, 80, (uint8_t *)"r/s", WHITE, BLACK, 16, 0);

    LCD_ShowFloatNum1(68, 100, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(124, 100, (uint8_t *)"->", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(140, 100, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(196, 100, (uint8_t *)"rad", WHITE, BLACK, 16, 0);

    LCD_ShowFloatNum1(68, 120, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(124, 120, (uint8_t *)"->", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(140, 120, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(196, 120, (uint8_t *)"N/m", WHITE, BLACK, 16, 0);
}
